<?php
/**
 * Helper to create more descriptive timestamps
 * 
 * @category Abovo
 * 
 * @package Abovo_View
 * 
 * @author Antti Holvikari <anttih@gmail.com>
 * 
 * @license http://opensource.org/licenses/bsd-license.php BSD
 */
 
/**
 * Helper to create more descriptive timestamps
 * 
 * From smallest to biggest:
 * 
 * * seconds ago
 * * 1 minute ago
 * * x minutes ago
 * * 1 hour ago
 * * x hours ago
 * * yesterday
 * * x days ago
 * * 1 month ago
 * * x months ago
 * 
 * @category Abovo
 * 
 * @package Abovo_View
 */
class Abovo_View_Helper_TimestampNice extends Solar_View_Helper {
    
    /**
     * undocumented class variable
     *
     * NOTE: "second" is not one of them
     * 
     * @var string
     */
    private $_levels = array('auto', 'minute', 'hour', 'day', 'month');
    
    /**
     * 
     *
     * @var array
     */
    private $_time = array();
    
    /**
     * undocumented class variable
     *
     * @var string
     */
    private $_locale = 'PAST_';
    
    /**
     * Creates a "nice" timestamp
     *
     * @param mixed $spec Timestamp in any form suitable for strtotime()
     * 
     * @return string
     */
    public function timestampNice($spec, $level = 'auto')
    {
        // parse to a unix timestamp
        $unix = strtotime($spec);
        
        // calculate difference in seconds
        $diff = time() - $unix;
        
        // past or future?
        if ($diff < 0) {
            // using 'FUTURE_*' locale strings
            $this->_locale = 'FUTURE_';
            // only a positive value
            $diff *= -1;
        }
        
        $this->_time = array(
            'minute' => floor($diff / 60),
            'hour'   => floor($diff / (60 * 60)),
            'day'    => floor($diff / (60 * 60 * 24)),
            'month'  => floor($diff / (60 * 60 * 24 * 30)),
        );
        
        if (! in_array((string) $level, $this->_levels)) {
            $level = 'auto';
        }
        
        if ($level == 'auto') {
            return $this->_auto();
        } else {
            return $this->_level($level);
        }
    }
    
    /**
     * undocumented function
     *
     * @return void
     */
    protected function _auto()
    {
        foreach (array_reverse($this->_time) as $key => $val) {
            if ($val > 0) {
                if ((int) $val === 1) {
                    // singular
                    return Solar::$locale->fetch(
                        'Abovo_Date', 
                        $this->_locale . strtoupper($key)
                    );
                }
                // plural
                return sprintf(
                    Solar::$locale->fetch(
                        'Abovo_Date',
                        $this->_locale . strtoupper($key), $val
                    ),
                    $val
                );
            }
        }
        
        // it is within a few seconds
        return Solar::$locale->fetch('Abovo_Date', $this->_locale . 'SECOND');
    }
    
    /**
     * undocumented function
     *
     * @return void
     */
    protected function _level($level)
    {
        Solar::dump($this->_time);
        // singular of plural?
        if ($this->_time[$level] == 1) {
            return Solar::$locale->fetch('Abovo_Date', $this->_locale . strtoupper($level));
        }
        
        // plural
        return sprintf(
            Solar::$locale->fetch(
                'Abovo_Date',
                $this->_locale . strtoupper($level)
            ),
            $this->_time[$level]
        );
    }
}
